//
//  WBRootController.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/6.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit
import MJRefresh

class WBRootController: UIViewController {
    
    var visitorInfo: [String: Any]?
    var visitorView: WBVisitorView?
    
    /// 下拉刷新的控件
    lazy var refreshHeader: MJRefreshNormalHeader = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadData))
    
    lazy var refreshFooter: MJRefreshAutoNormalFooter = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadData))
    
    
    /// tableview的懒加载属性
    lazy var tableView: UITableView = {
    
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        //自适应行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        //tableView.register(UITableViewCell.self, forCellReuseIdentifier: String)
        tableView.mj_header = self.refreshHeader
        tableView.mj_footer = self.refreshFooter
        return tableView
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        //接收登录成功的通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess(notification:)), name: loginSuccessNotificationName, object: nil)
        
        self.setupUI()
    }
    
    fileprivate func setupUI() {
        //1. 先添加tableView
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
        
        //2. 设置访客视图， 盖住tableView
        //如果没有登录，则显示访客视图，如果登录就不显示  testDebug
        if (!WBUserAccount.shared.isLogin) {
            visitorView = WBVisitorView(frame: self.view.bounds)
            visitorView?.delegate = self
            visitorView?.visitorInfo = self.visitorInfo
            self.view.addSubview(visitorView!)
        }
    }
}

extension WBRootController: WBVisitorViewDelegate {
    func login() {
        //推出一个登录的控制器
        let loginvc = WBLoginController()
        let loginNav = UINavigationController(rootViewController: loginvc)
        present(loginNav, animated: true, completion: nil)
    }
}

extension WBRootController {
    /// 登录成功之后的通知的处理事件@objc
    @objc func loginSuccess(notification: Notification) {
        //将访客视图从子控制器移除
        visitorView?.removeFromSuperview()
        visitorView = nil
        
        //刷新数据
        loadData()
    }
}

// 数据加载
extension  WBRootController {
    @objc func loadData() {
    
    }
}

extension WBRootController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 0
//    }
}
